import logging

import pytest

from resource.pom.settings import Settings
from utils.manager import Images


class Test:
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面，点击设置-蓝牙，点击关闭蓝牙按钮')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.click(*self.app.bluetooth_item)
        device.refresh_layout()
        switch = device.get_element_by_type('Toggle')
        # wifi_status = device.get_wifi_status()
        if switch.get('checked') == 'true':
            device.click_element(switch)
        else:
            device.click_element(switch)
            device.wait(5)
            device.click_element(switch)
        logging.info('预期结果1.蓝牙按钮不亮，通知栏不再显示蓝牙的图标，不显示可用设备列表，已连接的蓝牙被取消')
        device.wait(3)
        device.refresh_layout()
        switch = device.get_element_by_type('Toggle')
        assert switch.get('checked') == 'false'
        device.assert_text_exist('可用设备', expect=False)
        element_area = device.get_element_area(switch)
        blue_tooth_off = device.save_snapshot_to_local('blue_tooth_off.jpeg')
        blue_tooth_off = Images.crop_picture(blue_tooth_off, *element_area)
        similarity = Images.compare_image_similarity(blue_tooth_off, self.app.switch_off)
        assert similarity > 0.9, '界面上蓝牙开关未显示为关闭状态'
